<template>
  <div class="header">
    <div class="header-top">
      <div class="top-left">
        尚品汇欢迎您！
        <span v-if="name">
          {{ name }}
          <span @click="logout">退出登录</span>
        </span>
        <span v-else>
          请
          <router-link to="/login">登录</router-link>
          |
          <router-link to="/register">免费注册</router-link>
        </span>
      </div>
      <ul class="top-right">
        <li><router-link to="/center">我的订单</router-link></li>
        <li><router-link to="/shopCart">我的购物车</router-link></li>
        <li>我的尚品汇</li>
        <li>尚品汇会员</li>
        <li>企业采购</li>
        <li>关注尚品汇</li>
        <li>合作招商</li>
        <li>商家后台</li>
      </ul>
    </div>
    <div class="header-bottom">
      <router-link to="/">
        <img src="./logo.png" alt="logo" />
      </router-link>
      <form action="#">
        <input type="text" v-model="keyword" />
        <button @click.prevent="toSearch">搜索</button>
      </form>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  name: 'Header',
  // 给搜索框绑定双向数据
  data() {
    return {
      keyword: '',
    }
  },
  computed: {
    ...mapState('user', ['name']),
  },
  methods: {
    ...mapActions('user', ['logout']),
    toSearch() {
      // 点击搜索的时候判断一下,keywords是否是空,如果不是,就添加到params上,如果是空,则不添加
      const { keyword } = this
      // 这里加query是为了保证查询字符串和params能共同存在,反过来,在三级菜单跳转到search的时候,也要携带params,所以,TypeNav跳转的时候也要添加参数params
      const location = {
        name: 'Search',
        query: this.$route.query,
      }
      if (keyword) {
        location.params = {
          keyword,
        }
      }
      this.$router.history.push(location)
    },
  },
}
</script>

<style lang="less" scoped>
.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 30px;
  padding: 0 20px;
  background-color: #eaeaea;
}
.top-right {
  display: flex;
  li {
    margin-left: 10px;
    padding-right: 10px;
    border-right: 1px solid #000;
    &:last-child {
      border-right: none;
    }
  }
}
.header-bottom {
  width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  img {
    margin-left: 20px;
  }

  form {
    display: flex;
    input {
      border: 1px solid red;
      outline: none; //清除input框的默认选中样式
      height: 35px;
      width: 500px;
      padding-left: 10px;
      font-size: 16px;
      box-sizing: border-box;
    }
    button {
      border: none;
      background-color: red;
      height: 35px;
      width: 80px;
      font-size: 16px;
      color: #fff;
      cursor: pointer;
    }
  }
}
</style>
